﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Gift.Model;
using Gift.DAL;

namespace WebSite.admin.product
{
    public partial class modelType : BaseAdmin
    {
        readonly DALModelType theModelType = new DALModelType();
        readonly DALGoods theGoods = new DALGoods();
        tbModelType oModelType;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.DeleteModelType();
                this.BuidForm();
            }
        }

        void DeleteModelType()
        {
            if (Request.QueryString["mid"] != null && Request.QueryString["action"] == "del")
            {
                theModelType.Delete(int.Parse(Request.QueryString["mid"]));
                ExtAspNet.Alert.Show("删除成功！");
                
            }
        }

        void BuidForm()
        {
            this.txtGoodsPoints.Text = "";
            this.txtGoodsWeight.Text = "";
            this.txtModelTypeName.Text = "";
            this.txtOrder.Text = "";

            string sWhere = "";

            if (Request.QueryString["gid"] != null)
            {
         
                    sWhere = " goodsId=" + Request.QueryString["gid"];

            }
            else
            {
                if (Session["goodsCode"] != null)
                {
                    sWhere = " goodsCode=" + Session["goodsCode"].ToString();
                }
 
            }
            if (sWhere != "")
            {
                sWhere = sWhere + " and isnull(isDeleted,0)=0 ";
            }
            DataTable table = theModelType.GetList(sWhere).Tables[0];
            this.Grid1.DataSource = table;
            this.Grid1.DataBind();
        }

        protected void btnSave01_Click(object sender, EventArgs e)
        {
            oModelType = new tbModelType();
            if (Request.QueryString["gid"] == null)
            {
                if (Session["goodsCode"] != null)
                {
                    oModelType.goodsCode = Session["goodsCode"].ToString();
                }
                else
                {
                    ExtAspNet.Alert.Show("系统异常，请重新添加商品！");
                    return;
                }
            }
            else
            {
                oModelType.goodsId = int.Parse(Request.QueryString["gid"]);
            }
            if (this.txtGoodsWeight.Text.Trim() == "")
            {
                oModelType.goodsWeight = 0;
            }
            else
            {
                oModelType.goodsWeight = decimal.Parse(this.txtGoodsWeight.Text);
            }
            oModelType.modelTypeName = this.txtModelTypeName.Text.Trim();
            oModelType.goodsPoints = int.Parse(this.txtGoodsPoints.Text);
            oModelType.goodsPrice = decimal.Parse(this.txtGoodsPrice.Text);
            oModelType.modelTypeSort = int.Parse(this.txtOrder.Text);
            theModelType.Add(oModelType);
            this.BuidForm();

        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
           ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHidePostBackReference());
           // ExtAspNet.ActiveWindow.GetHidePostBackReference();
        }
    }
}